package com.edais.service.imp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.edais.dao.UserVouchersDao;
import com.edais.domain.ChannelWelfareParameters;
import com.edais.domain.UserVouchers;
import com.edais.domain.VouchersRule;
import com.edais.service.WelfareService;

/**
 * 代金券福利发放
 * @author zhouyj
 *
 */
@Service("voucherWelfareServiceImpl")

public class VoucherWelfareServiceImpl implements WelfareService {

	private static final Log logger = LogFactory.getLog(VoucherWelfareServiceImpl.class);

	@Autowired
	private UserVouchersDao userVouchersDao;
	
	
	@Override
	public Map<String,Object> addWelfare(ChannelWelfareParameters channelWelfareParameters,Map<String,Object> paramsMap) throws ParseException {
		Integer userId = paramsMap.get("userId")==null?null:Integer.valueOf(paramsMap.get("userId").toString());
		logger.info("******发放代金券福利，福利参数id："+channelWelfareParameters.getId()+",发放用户"+userId+"********");
		
		paramsMap.put("voucherId", channelWelfareParameters.getVouchersId());
		VouchersRule vouchersRule = userVouchersDao.queryVouchersRuleById(paramsMap);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date nowDate = sdf.parse(sdf.format(new Date()));
		Calendar cal = Calendar.getInstance();
		cal.setTime(nowDate);
		cal.add(Calendar.DATE, channelWelfareParameters.getExpires()-1);
		Date validityPeriodDate = cal.getTime();
		//代金券
		UserVouchers userVouchers = new UserVouchers();
		userVouchers.setUser_id(Long.valueOf(userId));
		userVouchers.setVoucher_type_id(Long.valueOf(channelWelfareParameters.getVouchersId()));
		userVouchers.setStartdate(nowDate);
		userVouchers.setEnddate(validityPeriodDate);
		userVouchers.setVoucher_amount(vouchersRule.getVoucher_amount());
		userVouchers.setInvestment_amount(vouchersRule.getInvestment_amount());
		userVouchers.setSource("2");
		userVouchers.setGift_staff("sysadmin");
		userVouchers.setCreate_time(new Date());
		userVouchers.setStatus("0");
		userVouchers.setCreate_per("javaservice");
		userVouchersDao.addUserVouchers(userVouchers);
		return null;
	}
	


}
